package com.link.base.base.appduty.service;


import com.link.base.base.appduty.dao.mybatis.mapper.AppDutyMenuInterMapper;
import com.link.base.base.appduty.model.AppDutyMenuInter;
import com.link.base.base.appduty.model.AppQuickSetVO;
import com.link.base.base.duty.model.Duty;
import com.link.base.base.monitor.MonitorObjectChange;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import javax.annotation.Resource;

import com.link.core.basic.service.KeyGenerateService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title</p>
 * <p>Description</p>
 * <p>Company</p>
 *
 * @author yrf
 * @date 2016年8月4日  下午4:14:22
 */

@Service
public class AppDutyMenuInterServiceImpl extends BasicServiceImpl<AppDutyMenuInter> implements AppDutyMenuInterService {

    @Resource
    private AppDutyMenuInterMapper appDutyMenuInterMapper;
    @Resource
    private MonitorObjectChange monitorObjectChange;
    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicMapper<AppDutyMenuInter> getBasicMapper() {
        return appDutyMenuInterMapper;
    }


    @Override
    public void beforUpsert(AppDutyMenuInter appDutyMenuInter) throws Exception {
        // 这里需要监控职责url变换,当变化时需要触发职责url变化
        Duty duty = new Duty();
        duty.setId(appDutyMenuInter.getDutyId());
        monitorObjectChange.dutyChange(duty,"APP");
    }

    @Override
    public void beforDelete(AppDutyMenuInter appDutyMenuInter) throws Exception {
        // 这里需要监控职责url变换,当变化时需要触发职责url变化
        Duty duty = new Duty();
        duty.setId(appDutyMenuInter.getDutyId());
        monitorObjectChange.dutyChange(duty,"APP");
    }

    @Override
    public Map<String, Object> quickSetUpMenuDuty(AppQuickSetVO entity) throws Exception {
        List<AppDutyMenuInter> addMenuList = entity.getAddMenuList();
        List<AppDutyMenuInter> delMenuList = entity.getDelMenuList();
        long dutyId = entity.getDutyId();
        if ("null".equals(String.valueOf(dutyId))) {
            throw new BasicServiceException("职责id不能为空");
        }
        Map<String, Object> result = new HashMap<>(8);
        // 循环插入新增的菜单数据
        if (null != addMenuList && addMenuList.size() > 0) {
            for (AppDutyMenuInter appDutyMenuInter : addMenuList) {
                if ("null".equals(String.valueOf(appDutyMenuInter.getId()))) {
                    appDutyMenuInter.setId(keyGenerateService.keyGenerate());
                }
                appDutyMenuInterMapper.insert(appDutyMenuInter);
            }
        }
        // 循环删除菜单数据
        if (null != delMenuList && delMenuList.size() > 0) {
            for (AppDutyMenuInter appDutyMenuInter : delMenuList) {
                AppDutyMenuInter item = appDutyMenuInterMapper.queryByExamplePage(appDutyMenuInter).get(0);
                if (null != item) {
                    appDutyMenuInterMapper.deleteById(item);
                }
            }
        }
        // 获取当前职责对应的职责-菜单中间表记录
        AppDutyMenuInter appMenuSecInter = new AppDutyMenuInter();
        appMenuSecInter.setDutyId(dutyId);
        appMenuSecInter.setPageFlag(false);
        List<AppDutyMenuInter> list = appDutyMenuInterMapper.queryByExamplePage(appMenuSecInter);

        result.put("result", list);
        return result;
    }
}
